let express = require("express");
let qs = require("querystring");
let register = express.Router();
let { pool } = require("../db/pool");
// var session = require('express-session');
// const { connect } = require('http2');
var Event = require("events").EventEmitter;
var query = new Event();
// let bodyParser = require('body-parser')
register.get("/register", (req, res) => {
  console.log(req.query);
  let id = Math.random() * 1000 + 10000;
  let username = req.query.username;
  let password = req.query.password;
  let telephone = req.query.telephone;
  let gender = req.query.gender;
  let user_face = req.query.uploader[0].url;
  // console.log(username,password,telephone,gender)
  pool.getConnection((err, connection) => {
    if (err) throw err;
    connection.query(
      `select user_name from base_user where user_name='${username}'`,
      (err, result) => {
        if (err) {
          throw err;
        }
        if (result) {
          if (!result.length) {
            connection.query(
              `INSERT INTO base_user (user_id,user_name,user_password,telephone,gender,user_face) VALUES ('${id}','${username}','${password}','${telephone}','${gender}','${user_face}')`,
              (err, result) => {
                if (err) {
                  throw err;
                }
                console.log(result);
                res.send({ status: 200, message: "OK" });
              }
            );
          } else {
            res.send({ status: 300, message: "用户已存在" });
          }
        }
        connection.release() 
      }
    );
  });
});
module.exports = { register };
